const db = wx.cloud.database()
const app = getApp()
const _ = db.command
Page({

  /**
   * 页面的初始数据
   */
  data: {

  },

  onLoad(options) {
    this.getMemberNotAdmit()
  },

  getMemberNotAdmit() {
    db.collection("all_team").where({
        _openid: app.globalData.openid
      }).get()
      .then(res => {
        console.log("获取待录取进入团队的志愿者列表成功", res)
        this.setData({
          memberNotAdmit: res.data[0].memberNotAdmit
        })
      }).catch(err => {
        console.log("获取待录取进入团队的志愿者列表失败", err)
      })
  },

  admit(e) {
    let vlnid = e.currentTarget.dataset.id
    console.log("对应申请人的_id", vlnid)
    db.collection("all_team").where({
      _openid: app.globalData.openid
    }).update({
      data:{
        memberNotAdmit: _.pull({
          _id: vlnid
        }),
      }
    }).then(res => {
      console.log("把该志愿者从待录取列表中丢掉成功", res)
    }).catch(err => {
      console.log("把该志愿者从待录取列表中丢掉失败", err)
    })

    //拿到待录取列表里对应的志愿者
    let index = this.data.memberNotAdmit.findIndex(e => e._id == vlnid)
    console.log(this.data.memberNotAdmit[index])
    let tempVln = this.data.memberNotAdmit[index]

    db.collection("all_team").where({
      _openid: app.globalData.openid
    }).update({
      data:{
        memberAdmit: _.push({
          avar:tempVln.avar,
          name:tempVln.name,
          openid:tempVln.openid,
          _id:tempVln._id
        }),
      }
    }).then(res => {
      console.log("把该志愿者放进已录取列表成功", res)

      wx.cloud.callFunction({
        name: "sendSubptMessage",
        data: {
          openId: tempVln.openid,
          projectName: "审核您的报名加入组织申请",
          condition: "审核通过",
          userName: tempVln.name,
          teamNickname: app.globalData.teamInfo.teamInfo.teamName,
          phoneNum: app.globalData.teamInfo.managerInfo.phone,
          flag: "judgeProject"
        }
      }).then(res => {
        console.log("发送订阅消息云函数调用成功", res)
      })
      this.getMemberNotAdmit()
    }).catch(err => {
      console.log("把该志愿者放进已录取列表失败", err)
    })
  },
  refuse(e) {
    let vlnid = e.currentTarget.dataset.id
    let index = this.data.memberNotAdmit.findIndex(e => e._id == vlnid)
    console.log(this.data.memberNotAdmit[index])
    let tempVln = this.data.memberNotAdmit[index]
    
    console.log("对应申请人的_id", vlnid)
    db.collection("all_team").where({
      _openid: app.globalData.openid
    }).update({
      data:{
        memberNotAdmit: _.pull({
          _id: vlnid
        }),
      }
    }).then(res => {
      console.log("把该志愿者从待录取列表中丢掉成功", res)

      wx.cloud.callFunction({
              name: "sendSubptMessage",
              data: {
                openId: tempVln.openid,
                projectName: "审核您的报名加入组织申请",
                condition: "审核未通过",
                userName: tempVln.name,
                teamNickname: app.globalData.teamInfo.teamInfo.teamName,
                phoneNum: app.globalData.teamInfo.managerInfo.phone,
                flag: "judgeProject"
              }
            }).then(res => {
              console.log("发送订阅消息云函数调用成功", res)
            })
            this.getMemberNotAdmit()
    }).catch(err => {
      console.log("把该志愿者从待录取列表中丢掉失败", err)
    })
  },
})